package pos.data;

import java.util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import Clases.domain.Barrio;
import Clases.domain.Zona;

public class JDBCBarrioDAO implements IBarrioDAO {
	private Connection conn;
	IZonaDAO zona;

	public JDBCBarrioDAO() {
		zona = new JDBCZonaDAO();
		conn = ConnectionManager.getInstance().checkOut();
	}
	protected void finalize() {
		ConnectionManager.getInstance().checkIn(conn);
	}

	public List selectAllBarrios() {
		
		PreparedStatement stmt = null;
		List barrios = new LinkedList();
		ResultSet result = null;
		String sql = "SELECT nombre,barrioOID FROM Barrio";

		try {

			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			while (result.next()) {

				Barrio b = new Barrio();
				List zonas = (List) zona.selectZonasBarrio(conn, result.getInt("barrioOID"));

				b.setNombre(result.getString("nombre"));
				b.setZonas(zonas);
				barrios.add(b);

			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			this.finalize();
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return barrios;
	}

}